package com.rayo.accesscontrollercopy.model.db.dao;

import com.litesuits.orm.db.assit.QueryBuilder;
import com.rayo.accesscontrollercopy.model.db.DbHelp;
import com.rayo.accesscontrollercopy.model.db.bean.SiteData;

import java.util.Date;
import java.util.List;

/**
 * Created by yanghaojie on 16/7/27.
 */
public class SiteImpl implements SiteDao {
    @Override
    public List<SiteData> findAll() {
        return DbHelp.getQueryAll(SiteData.class);
    }

    @Override
    public synchronized List<SiteData> findBySite(String site) {
        return DbHelp.getQueryByWhere(SiteData.class, SiteData.SITE, new String[]{site});
    }

    @Override
    public SiteData findFirst() {
        List<SiteData> list = findAll();
        if (null == list || 0 == list.size())
            return new SiteData();
        else
            return list.get(0);
    }

    @Override
    public SiteData findBySiteAndId(String site, int lockid) {
        QueryBuilder qb = new QueryBuilder(SiteData.class).whereEquals(SiteData.SITE, site)
                .whereAnd(SiteData.LOCK_ID, new String[]{String.valueOf(lockid)});
        List<SiteData> list = DbHelp.liteOrm.query(qb);
        return 0 == list.size() ? null : list.get(0);
    }

    @Override
    public void addSite(SiteData siteData) {
        siteData.setEditime(new Date());
        DbHelp.insert(siteData);
    }

    @Override
    public void updateSite(SiteData siteData) {
        siteData.setEditime(new Date());
        DbHelp.update(siteData);
    }

    @Override
    public void deleteSite(SiteData siteData) {
        DbHelp.delete(siteData);
    }
}

